/**
 * Created by hiibook on 2017/6/8.
 */
var crypto =require("crypto");
//微信请求验证
var config = require("../config/config");
var wechatVerification = function (req, res, next) {
    console.log('微信get请求判断');
    var echostr, nonce, signature, timestamp;
    signature = req.query.signature;
    timestamp = req.query.timestamp;
    nonce = req.query.nonce;
    echostr = req.query.echostr;
    if (check(timestamp, nonce, signature, config.wechatToken)) {
        res.end(echostr);
    } else {
        console.log('非微信请求,结束');
        res.end();
    }
};

function check(timestamp, nonce, signature, token) {
    var currSign, tmp;
    tmp = [token, timestamp, nonce].sort().join("");
    currSign = crypto.createHash("sha1").update(tmp).digest("hex");
    return currSign === signature;
};
module.exports = wechatVerification;